<?php
/**
 * 图片上传模型
 */
namespace common\models;

/**
 * @property string $image
 * @property string $file
 * @property string $video
 */
class Assets extends \yii\db\ActiveRecord
{
    //场景
    const SCENARIO_ICO = 'ico';//系统ico
    const SCENARIO_LOGO = 'logo';//系统后台logo
    const SCENARIO_OTHER_IMAGE = 'other_image';//其他图片
    const SCENARIO_ARTICLE = 'article';//文章
    const SCENARIO_GOODS = 'goods';//商品
    const SCENARIO_GOODS_DESC = 'goods_desc';//商品描述
    const SCENARIO_USER_AVATAR = 'user_avatar';//后台用户头像
    const SCENARIO_MEMBER_AVATAR = 'member_avatar';//会员头像
    const SCENARIO_VIDEO = 'video';//视频
    const SCENARIO_WORKS = 'works';//作品
    const SCENARIO_QRCODE = 'qrcode';//二维码场景

    public $image;

    public function rules()
    {
        return [
            //ico，必须32*32的ico格式图片
            ['image', 'image', 'extensions' => 'ico', 'on' => self::SCENARIO_ICO],
            //后台logo,必须252*92的图片
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_LOGO],
            //其他图片
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_OTHER_IMAGE],
            //文章
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_ARTICLE],
            //商品
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_GOODS,
                'minWidth' => 800,
                'minHeight' => 800,
                'maxWidth' => 800,
                'maxHeight' => 800,
            ],
            //商品描述
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_GOODS_DESC],
            //user头像
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_USER_AVATAR],
            //member头像
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_MEMBER_AVATAR,
                'minWidth' => 200,
                'minHeight' => 200,
                'maxSize'=> 2*1024*1024
            ],
            //视频
            ['video', 'file', 'extensions' => 'mp4', 'on' => self::SCENARIO_VIDEO, 'maxSize' => 1024 * 1024 * 10],
            //蛋糕作品
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_WORKS,
                'minWidth' => 500,
                'minHeight' => 500,
                'maxSize'=> 2*1024*1024
            ],
            //二维码
            ['image', 'image', 'extensions' => 'png, jpg, jpeg', 'on' => self::SCENARIO_QRCODE,
                'minWidth' => 300,
                'minHeight' => 300,
                'maxSize'=> 2*1024*1024
            ],
        ];
    }

    /**
     * 场景
     * @return array
     */
    public function scenarios()
    {
        //继承默认场景
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_ICO] = ['image'];//ico
        $scenarios[self::SCENARIO_LOGO] = ['image'];//后台logo
        $scenarios[self::SCENARIO_OTHER_IMAGE] = ['image'];//其他图片
        $scenarios[self::SCENARIO_ARTICLE] = ['image'];//文章
        $scenarios[self::SCENARIO_GOODS] = ['image'];//商品
        $scenarios[self::SCENARIO_GOODS_DESC] = ['image'];//商品描述
        $scenarios[self::SCENARIO_USER_AVATAR] = ['image'];//后台用户头像
        $scenarios[self::SCENARIO_MEMBER_AVATAR] = ['image'];//会员头像
        $scenarios[self::SCENARIO_VIDEO] = ['video'];//视频
        $scenarios[self::SCENARIO_QRCODE] = ['image'];//视频

        return $scenarios;
    }

    public function attributeLabels()
    {
        return [
            'image' => '图片文件',
            'video' => '视频文件',
            'file' => '文件',
        ];
    }

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'assets';
    }

}